/**
 * Creation Date:2017年8月16日-上午11:10:57
 * 
 * Copyright 2008-2017 © 同程网 Inc. All Rights Reserved
 */
package com.dabaozhan.common.utils;

import java.util.Optional;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;

import org.springframework.util.CollectionUtils;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;

public class ValidationBeanUtils {
	
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    
    /**
     * 验证对象属性是否为空
     */
    public static <T> void validateBeanRequestDto(T obj) {
        Set<ConstraintViolation<T>> set = validator.validate(obj, Default.class);
        if (!CollectionUtils.isEmpty(set)) {
            Optional<ConstraintViolation<T>> op = set.parallelStream().findFirst();
            if (op.isPresent()) {
                ConstraintViolation<T> cv = op.get();
                throw new RRException(ResponseCodeEnum.ResponseCode_101.getCode(), cv.getMessage());
            }
        }
    }


   
}

